package com.example.mybatisplus.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.mybatisplus.common.utls.SessionUtils;
import com.example.mybatisplus.model.domain.WhitelistSetting;
import com.example.mybatisplus.mapper.WhitelistSettingMapper;
import com.example.mybatisplus.model.dto.PageDTO;
import com.example.mybatisplus.service.WhitelistSettingService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Objects;

import static org.apache.poi.hssf.model.InternalSheet.createSheet;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fmx
 * @since 2024-07-14
 */
@Service
public class WhitelistSettingServiceImpl extends ServiceImpl<WhitelistSettingMapper, WhitelistSetting> implements WhitelistSettingService {

    @Override
    public WhitelistSetting login(WhitelistSetting whitelistSetting) {
        QueryWrapper<WhitelistSetting> wrapper = new QueryWrapper<>();
        wrapper.eq("sn",whitelistSetting.getSn())
                .eq("password",whitelistSetting.getPassword())
                .eq("is_enabled",true);
        WhitelistSetting one =baseMapper.selectOne(wrapper);// 这个base就是白名单mapper 故可以不注入
        // 登录时把登录用户存到session
        /*if(one != null){
            SessionUtils.saveCurrentUser(one);
        }*/
        return one;
    }

    @Override
    public Page<WhitelistSetting> pageList(WhitelistSetting whitelistSetting, PageDTO dto) {
        QueryWrapper<WhitelistSetting> wrapper = new QueryWrapper<>();
        if(StringUtils.isNotBlank(whitelistSetting.getSn())){// StringUtils认准lang3
            wrapper.like("sn", whitelistSetting.getSn());
        }
        // 和上面效果一样
//        if(whitelistSetting.getSn() != null && whitelistSetting.getSn() != ""){
//            wrapper.like("sn", whitelistSetting.getSn());
//        }
        if(whitelistSetting.getName() != null && whitelistSetting.getName() != ""){
            wrapper.like("name", whitelistSetting.getName());
        }
        if(Objects.nonNull(whitelistSetting.getRoleId())){
            wrapper.like("role_id", whitelistSetting.getRoleId());
        }
//        if(whitelistSetting.getRoleId() != null){
//            wrapper.like("role_id", whitelistSetting.getRoleId());
//        }
        Page<WhitelistSetting> page = new Page<>();
        page.setCurrent(dto.getPageNo()).setSize(dto.getPageSize());
        baseMapper.selectPage(page, wrapper);
        return page;
    }

    @Override
    public void exportUser(HttpServletResponse response) {
        // 查询所有用户
        List<WhitelistSetting> whitelistSettings = baseMapper.selectList(null);
        // xls xlsx
        Workbook workbook = new HSSFWorkbook();
        // 创建工作表对象
        Sheet sheet = workbook.createSheet("第一个工作表");
        // 行对象
        Row row = sheet.createRow(0);
        // 行内单元格
        Cell cell = row.createCell(0);
        cell.setCellValue("学号/工号");

        cell = row.createCell(1);
        cell.setCellValue("姓名");

        cell = row.createCell(2);
        cell.setCellValue("密码");

        cell = row.createCell(3);
        cell.setCellValue("角色");

        for(int i = 0; i < whitelistSettings.size(); i++){
            row = sheet.createRow(i+1);

            cell = row.createCell(0);
            cell.setCellValue(whitelistSettings.get(i).getSn());

            cell = row.createCell(1);
            cell.setCellValue(whitelistSettings.get(i).getName());

            cell = row.createCell(2);
            cell.setCellValue(whitelistSettings.get(i).getPassword());

            cell = row.createCell(3);
            cell.setCellValue(whitelistSettings.get(i).getRoleId());
        }

        try {
            workbook.write(response.getOutputStream());
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    // 导入功能一定要做下载模板
    @Override
    public void importUser(MultipartFile file) throws IOException {
        // 验证文件 xls xlsx
        // 根据后缀名获取对应poi对象
        Workbook workbook = new HSSFWorkbook(file.getInputStream());
        // 验证表头
        // 姓名学号角色
        // List<对象>
        // 判重： 表格内部判重 表格内容和数据库数据判重
        // 1.不存在的数据 插入 2.存在的数据 修改
        // 2.不存在的数据 插入 2.存在的数据 返回给前端
        // 3.解析出的数据全部返回给前端
        // poi单元格下拉
    }
}
